package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.Annunciate;
import com.qf.entity.Logs;
import com.qf.service.LogsService;
import com.qf.utils.AnnunciateExcel;
import com.qf.utils.LogsExcel;
import com.sun.deploy.net.HttpResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 2019/12/12
 * qjh
 * springboot_oa_five
 * 面向对象面向君 不负代码不负卿
 */
@Controller
public class LogController {

    @Autowired
    private LogsService logsService;

    @RequestMapping("/resource/system/log/list")
    @RequiresPermissions("logs:all")
    public String findAllLogs(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "")String userName, @RequestParam(defaultValue = "")String operTimeBegin, @RequestParam(defaultValue = "")String operTimeOver, ModelMap modelMap){
        PageInfo<Logs> allLogs = logsService.findAllLogs(page, userName, operTimeBegin, operTimeOver);
        modelMap.addAttribute("allLogs",allLogs);
        modelMap.addAttribute("operTimeBegin",operTimeBegin);
        modelMap.addAttribute("operTimeOver",operTimeOver);
        modelMap.addAttribute("userName",userName);
        return "resource/system/log/list";
    }

    @RequestMapping("/excelLogs")
    public void excelLogs(int[] arr, HttpServletResponse response) throws IOException {
        //导出数据
        //1.查询被选中的数据信息
        List<Logs> logs = logsService.excelLogs(arr);
        //2.导出数据
        LogsExcel.createFirst("用户信息");
        LogsExcel.createOthers(logs);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
        String s = format.format(new Date());
        response.setContentType("application/x-download");
        String fileName=s+".xls";
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        LogsExcel.export(outputStream);

    }
}
